<?php
namespace app\index\controller;


use think\Controller;

Class Upapi extends Controller {
    private $APPID = '';
    private $AccessKeySecret = '';
    private $gateway = '';

    public function __construct($UPApiConfig){
        $APPID = $UPApiConfig['APPID'];
        $AccessKeySecret = $UPApiConfig['AccessKeySecret'];
        $gateway = $UPApiConfig['gateway'];

        if (empty($APPID)){
            exit('APPID cannot be empty');
        }
        if (empty($AccessKeySecret)){
            exit('AccessKeySecret cannot be empty');
        }
        if (empty($gateway)){
            exit('gateway cannot be empty');
        }

        $this->APPID = $APPID;
        $this->AccessKeySecret = $AccessKeySecret;
        $this->gateway = $gateway;
    }

    //下单API
    public function createOrder($params){
        return $this->postTo('/index.php/pay/api/createOrder',$params);
    }

    //订单查询
    public function inquiryOrder($params){
        return $this->postTo('/index.php/pay/api/inquiryOrder',$params);
    }

    //获取支持代币列表
    public function inquiryCoin($params){
        return $this->postTo('/index.php/pay/api/inquiryCoin',$params,0);
    }

    //下发订单创建
    public function createIssue($params){
        return $this->postTo('/index.php/pay/api/createIssue',$params,0);
    }

    //下发订单查询
    public function inquiryIssue($params){
        return $this->postTo('/index.php/pay/api/inquiryIssue',$params,0);
    }

    private function postTo($url,$params,$chkSign = 1){
        $params['signature'] = $this->createSign($params);

        $json_res = $this->http_request($this->gateway . $url,$params);
        $res = json_decode($json_res,true);
        //echo $json_res;
        if(isset($res['code'])) {
            if ($res['code'] == 1) {
                if($chkSign) {
                    $chkSign = $this->createSign($res['data']);
                    if ($chkSign != $res['data']['signature']) {
                        return $this->returnStatus(0, 'The returned data failed to verify the signature');
                    }
                }

                return $this->returnStatus(1, 'success', $res['data']);
            }
            return $this->returnStatus(0, $res['msg']);
        }else{
            return $this->returnStatus(0, $json_res);
        }
    }

    //创建签名
    public function createSign($params){
        if(isset($params['signature'])){
            unset($params['signature']);
        }
        ksort($params);
        $str = '';
        foreach ($params as $val){
            $str .= $val;
        }
        $sign = md5($str . $this->AccessKeySecret);
        return $sign;
    }

    //网络请求
    private function http_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_HTTPGET, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_TIMEOUT,10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    //返回参数
    private function returnStatus($code=1,$msg='success',$data=[]){
        return ['code'=>$code,'msg'=>$msg,'data'=>$data];
    }
}
